package hall

import (
	"sync"
	"webgame/internal/manager"
	"webgame/internal/model/entity/hall"

	"github.com/topfreegames/pitaya/v2/component"
)

var (
	OnlinePlayerManager *onlinePlayerManager
	onlinePlayerOnce    sync.Once
)

type onlinePlayerManager struct {
	manager.BaseManager
	accountBindSessionIdMap map[int]*hall.PlayerInfo //在线玩家sessionId | userId的对应列表
	// onLoginServerMap        map[string]string //玩家所在的登录服列表
	// onGameServerMap         map[string]string //玩家所在的游戏服列表
	onlineThread component.LogicThread
}

/*
*
新建一个在线玩家管理器
*/
func NewOnlinePlayerManager() *onlinePlayerManager {
	onlinePlayerOnce.Do(func() {
		OnlinePlayerManager = &onlinePlayerManager{
			accountBindSessionIdMap: make(map[int]*hall.PlayerInfo, 0),
			// onLoginServerMap:        make(map[string]string, 0),
			// onGameServerMap:         make(map[string]string, 0),
			onlineThread: component.LogicThread{
				DieChan: make(chan struct{}, 1),
			},
		}
	})

	return OnlinePlayerManager
}

func (o *onlinePlayerManager) Init() {

}

func (o *onlinePlayerManager) Shutdown() {

}

func (o *onlinePlayerManager) GetPlayerInfo(uid int) *hall.PlayerInfo {
	return o.accountBindSessionIdMap[uid]
}

func (o *onlinePlayerManager) BindPlayerInfo(uid int, playerInfo *hall.PlayerInfo) {
	o.accountBindSessionIdMap[uid] = playerInfo
}

func (o *onlinePlayerManager) RemovePlayerInfo(uid int) {
	delete(o.accountBindSessionIdMap, uid)
}

func (o *onlinePlayerManager) IsOnlinePlayer(uid int) bool {
	_, ok := o.accountBindSessionIdMap[uid]
	return ok
}
